package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.internal.LinkedHashTreeMap;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.AllUser;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.MyExpandableListView;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/8/9 17:37
 * 邮箱：zhanghuaiha@gmail.com
 *  分组信息
 */

public class GroupingActivity extends BaseActivity {
    private static final int CUT_USER = 4;
    private ExpandableListView expandableListView;
    private TextView title;
    private ImageView back;
//    public String[] groups = { "总后台", "武汉", "西安", "上海" };
//    public String[][] children = {
//            { "胡算林", "张俊峰", "王志军", "二人","胡算林", "张俊峰", "王志军", "二人" ,"胡算林", "张俊峰", "王志军", "二人" ,"胡算林", "张俊峰", "王志军", "二人" ,"胡算林", "张俊峰", "王志军", "二人" ,"胡算林", "张俊峰", "王志军", "二人" ,"胡算林", "张俊峰", "王志军", "二人" ,"胡算林", "张俊峰", "王志军", "二人" ,"胡算林", "张俊峰", "王志军", "二人"  },
//            { "李秀婷", "蔡乔", "别高", "余音" },
//            { "摊派新", "张爱明" },
//            { "马超", "司道光" }
//    };

    private Map<Integer,List<AllUser>> cList = new HashMap<>();
    private Map<String,String> pList = new HashMap<>();
    private List<String> mList = new ArrayList<>();
    private MyExpandableListView myExpandableListView;
    private String udid;
    private String versionCode;
    private String id;
    private Context mContext;
    private String[] groups;
    private List<AllUser> data;
    private String oid;


    @Override
    public void initView() {
        mContext = this;
        title = (TextView) findViewById(R.id.add_title);
        back = (ImageView) findViewById(R.id.back);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
    }

    @Override
    public void initData() {
        data = (List<AllUser>) getIntent().getSerializableExtra("date");
        int admin = getIntent().getIntExtra("admin", 0);
        if (admin == 0) {
            netWork();
        }else {
            getDate();
        }
        title.setText("用户分组");
        expandableListView.setOnChildClickListener((parent, v, groupPosition, childPosition, id1) -> {
            Intent intent = new Intent();
            intent.putExtra("new_eid",cList.get(groupPosition).get(childPosition).getId());
            intent.putExtra("new_user",cList.get(groupPosition).get(childPosition).getName());
            intent.putExtra("new_mobile",cList.get(groupPosition).get(childPosition).getMobile());
            intent.putExtra("new_admin",cList.get(groupPosition).get(childPosition).getAdmin());
            setResult(CUT_USER,intent);
            finish();
//            ToastUtil.showToast(cList.get(groupPosition).get(childPosition).getName());
            return true;
        });
    }

    private void netWork() {
        for (int i = 0; i < data.size(); i++) {
            pList.put(data.get(i).getCity_name(),data.get(i).getCity_name());
        }
        for (Map.Entry<String, String> stringStringEntry : pList.entrySet()) {
            mList.add(stringStringEntry.getKey());
        }

        for (int i = 0; i < mList.size(); i++) {
            List<AllUser> list = new ArrayList();

            for (AllUser user:data){
                if (mList.get(i).equals(user.getCity_name())) {
                    list.add(user);
                }
            }
            cList.put(i,list);
        }
        if (myExpandableListView == null) {
            myExpandableListView = new MyExpandableListView(mContext,mList, cList);
        }
        expandableListView.setAdapter(myExpandableListView);

    }

    public void getDate(){
        String tiem = System.currentTimeMillis() + "";
        udid = App.SP.getString("udid", "");
        versionCode = ObtainVersion.getVersionName(this);
        id = App.SP.getString("old_id", "");
        oid = App.SP.getString("oid", "");
        Map<String,String> map = new LinkedHashMap<>();
        map.put("rf","2");
        map.put("ts",tiem);
        map.put("v", versionCode);
        map.put("i", NetWorkCodeInfo.ALL);
        map.put("udid",udid);
        map.put("eid",id);
        map.put("oid",oid);
        String encry1 = ObtainEncry.getEncry(map);
        map.put("encry",encry1);
        RetrofitFactory.getmInstanse().mApi.getAllUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<List<AllUser>>>() {
                    @Override
                    public void success(BaseResponseEntity<List<AllUser>> listBaseResponseEntity) {
                        if (listBaseResponseEntity.code == 200) {
                            data = listBaseResponseEntity.data;
                            netWork();
                        }else{
                            DialogUtils.TSDialog(mContext,listBaseResponseEntity.message,true);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        DialogUtils.TSDialog(mContext,mContext.getString(R.string.error_date),true);
                    }
                });
    }

    @Override
    public void initListener() {
        back.setOnClickListener(v -> finish());
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_grouping;
    }
}
